﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 银行程序
{
    public partial class Form1 : Form
    {
        private Bank bank;
        private Account account=null;
        private Credit_Account credit = null;
        private bool is_credit=false;
        private ATM aTM ;
        public BigMoneyArgs bigMoneyArgs=null;

        public Form1(Bank bk,ATM atm)
        {
            InitializeComponent();
            bank = bk;
            aTM = atm;
        }

        private void register_button_Click(object sender, EventArgs e)
        {
            if (password_textbox.Text =="" || account_textbox.Text =="")
            {
                MessageBox.Show("请输入完整信息");
            }
            else
            {
                if (this.bank.search_account(long.Parse(account_textbox.Text))==null)
                {
                    Account a = new Account(long.Parse(account_textbox.Text), long.Parse(password_textbox.Text));
                    this.bank.Add_account(a);
                    MessageBox.Show("注册成功，您的账户是："+a.BankId);
                }
                else
                {
                    MessageBox.Show("该账号已被注册");
                    account_textbox.Text = "";
                    password_textbox.Text = "";
                }
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }




        //账号密码金额只能输入数字
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;//handle为已处理
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;//handle为已处理
            }
        }

        private void moneybox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;//handle为已处理
            }
        }




        private void login_button_Click(object sender, EventArgs e)
        {
            if (bank.search_account(int.Parse(account_textbox.Text)) == null)
            {
                MessageBox.Show("该账号不存在");
            }
            else
            {
                account = bank.search_account(int.Parse(account_textbox.Text));
                if (account.IsCredit)
                {
                    credit=bank.search_credit_account(int.Parse(account_textbox.Text));
                    is_credit = true;
                }

                if (account.Password == int.Parse(password_textbox.Text))
                {
                    MessageBox.Show("登录成功");
                    login_UI_change();
                }
                else
                {
                    MessageBox.Show("密码错误，登录失败");
                    password_textbox.Text = "";
                    account = null;
                    credit = null;
                }
            }
        }
        private void logout_button_Click(object sender, EventArgs e)
        {
            account_textbox.Text = "";
            password_textbox.Text = "";
            account = null;
            credit = null;
            is_credit=false;
            logout_UI_change();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void login_UI_change()
        {
            label2.Visible = false;
            password_textbox.Visible = false;
            login_button.Visible = false;
            register_button.Visible = false;
            label3.Visible = true;
            moneybox.Visible = true;               
            deposit_button.Visible = true;
            withdraw_button.Visible = true;
            checkAccount_button.Visible = true;
            logout_button.Visible = true;

        }

        private void logout_UI_change()
        {
            label2.Visible = true;
            password_textbox.Visible = true;
            login_button.Visible = true;
            register_button.Visible = true;
            label3.Visible=false;
            moneybox.Visible = false;
            deposit_button.Visible = false;
            withdraw_button.Visible = false;
            checkAccount_button.Visible = false;
            logout_button.Visible = false;
        }

        private void deposit_button_Click(object sender, EventArgs e)
        {
            if (moneybox.Text == "")
            {
                MessageBox.Show("请输入金额");
            }
            else
            {
                aTM.deposite_money(account, credit, long.Parse(moneybox.Text), is_credit);
                moneybox.Text = "";
            }
         }

        private void withdraw_button_Click(object sender, EventArgs e)
        {
            if (moneybox.Text == "")
            {
                MessageBox.Show("请输入金额");
            }
            else
            {
                aTM.withdraw_money(this ,account, credit, long.Parse(moneybox.Text), is_credit);
                moneybox.Text = "";
            }
        }

        private void checkAccount_button_Click(object sender, EventArgs e)
        {
            if (is_credit)
            {
                credit.Show_sum();
            }
            else
            {
                account.Show_sum();
            }
        }

        private void check_twice_button_Click(object sender, EventArgs e)
        {
            if (is_credit)//是信用卡的话
            {
                if (bigMoneyArgs.Big_Sum > credit.RestOfCredit)
                {
                    MessageBox.Show("超出信用额度，取款失败");
                }
                else
                {
                    credit.RestOfCredit = credit.RestOfCredit - bigMoneyArgs.Big_Sum;
                    MessageBox.Show("取款成功，您的账户信用余额为：" + credit.RestOfCredit);
                }

            }
            else//是非信用卡的话
            {
                if (bigMoneyArgs.Big_Sum > account.Sum)
                {
                    MessageBox.Show("超出账户余额，取款失败");
                }
                else
                {
                    account.Sum = account.Sum - bigMoneyArgs.Big_Sum;
                    MessageBox.Show("取款成功，您的账户余额为：" + account.Sum);
                }

            }
            this.bigMoneyArgs = null;
            cancel_button.Visible = false;
            check_twice_button.Visible = false;
            moneybox.Text = "";
        }

        private void cancel_button_Click(object sender, EventArgs e)
        {
            this.bigMoneyArgs = null;
            cancel_button.Visible = false;
            check_twice_button.Visible = false;
            moneybox.Text = "";
            MessageBox.Show("此次操作已取消");
        }
    }
}
